import { Service } from 'egg';
import { getOrgListByParent, createOrg, removeOrg, saveOrg } from '../sql/org';

export default class Org extends Service {
  async getOrgList(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(getOrgListByParent, params);
    ctx.logger.info('getOrgList:', sql, param);
    let res: any = await app.mysql.query(sql, param);
    res = res.map((it: any) => {
      const item = ctx.helper.toJsonObj(it) || {};
      return item;
    });
    return res;
  }
  async createOrg(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(createOrg, params);
    ctx.logger.info('createOrg:', sql, params);
    await app.mysql.query(sql, param);
    return null;
  }
  async removeOrg(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(removeOrg, params);
    ctx.logger.info('removeOrg:', sql, params);
    await app.mysql.query(sql, param);
    return null;
  }
  async saveOrg(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const formatParams = ctx.helper.toJsonString(params);
    const { sql, param } = ctx.helper.formatSql(saveOrg, formatParams);
    ctx.logger.info('saveOrg:', sql, param);
    await app.mysql.query(sql, param);
    return null;
  }
}
